﻿// A validation summary is a modal dialog that contains a grid with the validation errors 
require([
    'Wjs/ui/widgets/Dialog',
    'Wjs/ui/widgets/Grid'
],
function () {
    'use strict';

    return Wjs.defineOnce({

        type: 'validationSummary',

        internal: {

            'static': true,

            base: Wjs.getType('Dialog'),

            initialize: function () {
                var grid = Wjs.new('Grid')

                    .setWidth('100%')

                    .addColumn(

                        Wjs.new('GridColumn')

                            .setHeaderText('Error')

                            .setField('error')

                            .setWidth('100%')
                    );


                grid.getHeader().hide();

                this.setModal(true).setMovable(true);

                this.setTitle(

                    Wjs.new('Text')
                        .setText('Validation Errors')
                );

                this.addBodyItem(grid);

                this.render();
            },

            onTypeDefined: function (validationSummary) { // Register a default instance of the validator by alias

                Wjs.validationSummary = validationSummary; // Attach it to the global object
            }
        },

        setErrors: function (errors) {
            var grid = this.getBody().getItem(0);

            grid.getBody().clearItems();

            errors.forEach(function (error) {

                grid.getBody().addRow(error);
            });
        }
    });

});
